package controllers.admin;

import models.Admin.AdminLogins;
import models.System.SQL.SQL;
import models.Models.SystemRoteMeunName;
import models.System.Systems.SystemModels;
import models.SystemModels.*;
import models.interfaces.Models;
import models.interfaces.Systems;
import net.sf.json.JSONObject;
import play.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-06-26.
 */
public class table extends Logo{

    public static Systems systems = new models.System.Systems.Systems();

    public static void user_index(){
        List<SystemMeun> meun = new ArrayList<SystemMeun>();
        int size = SystemMeun.findAll().size();
        int number = size/5+1;
        int pageSize=0;
        try{

            pageSize = Integer.parseInt(params.get("pageSize"));
            meun= SystemMeun.all().from(pageSize*5).fetch((pageSize+1)*5);
            int i = meun.size();
            Logger.info(meun.size()+"");
            render(meun,size,number,pageSize,i);
        }catch (Exception ex){
            meun = SystemMeun.all().from(0).fetch(5);
            int i = meun.size();
            render(meun,size,number,pageSize,i);
        }

    }

    public static  void user_create(){
        String MenuName =  params.get("MenuName");
        String meunId =  params.get("meunId");
        try {
            StringBuffer sb =new StringBuffer();
            sb.append(SQL.ADMINMEUN);
            String sql = String.format("  meunId ='%s' or MenuName='%s'",MenuName,meunId);
            sb.append(sql);
            Logger.info(sb.toString());
            List<SystemMeun> list = SystemMeun.find(sql).fetch();
            Logger.info(list.size()+"");
            if (list.size()==0){
                SystemMeun meun = new SystemMeun();
                meun.MenuName= MenuName;
                meun.MeunId =meunId;
                meun.save();
                renderJSON(1);
            }else{
                renderJSON(-1);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            renderJSON(0);
        }
    }

    public static void user_del(){
        Long id = Long.parseLong(params.get("id"));
        try{
            SystemMeun systemMeun = SystemMeun.findById(id);
            systemMeun.delete();
            renderJSON(1);
        }catch (Exception ex){
            renderJSON(0);
        }
    }

    public static void user_edit(){
        Long id = Long.parseLong(params.get("id"));

        try{
            SystemMeun systemMeun = SystemMeun.findById(id);
            renderJSON(systemMeun);
        }catch (Exception ex){

            renderJSON(0);
        }
    }

    public static void user_edits(){
        Long id = Long.parseLong(params.get("id"));
        String MenuName =  params.get("MenuName");
        String MeunId = params.get("MeunId");
        try{
            SystemMeun systemMeun = SystemMeun.findById(id);
            systemMeun.MenuName = MenuName;
            systemMeun.MeunId = MeunId;
            systemMeun.save();
            renderJSON(1);
        }catch (Exception ex){

            renderJSON(0);
        }
    }

    /**
     * 权限分配
     */
    public static  void rote(){

        List<SystemRote> systemRotes = SystemRote.findAll();
        List<SystemMeun> systemMeunList = SystemMeun.findAll();

        List<SystemRoteMenu> meun = new ArrayList<SystemRoteMenu>();
        int size = SystemRoteMenu.findAll().size();
        int number = size/5+1;
        int pageSize=0;
        try{

            pageSize = Integer.parseInt(params.get("pageSize"));
            meun= SystemRoteMenu.all().from(pageSize*5).fetch((pageSize+1)*5);
            int i = meun.size();
            Logger.info(meun.size()+"&");

            render(meun,size,number,pageSize,i,systemRotes,systemMeunList);
        }catch (Exception ex){
            Logger.info(meun.size()+"&");

            meun = SystemRoteMenu.all().from(0).fetch(5);
            int i = meun.size();
            render(meun,size,number,pageSize,i,systemRotes,systemMeunList);
        }

    }
    public static void roteList(){
        Models db = new SystemModels();
        List<SystemRoteMeunName> list = new ArrayList<SystemRoteMeunName>();
        for (int e=0;e<db.systemRoteMeunName().size();e++){
            SystemRoteMeunName name = new SystemRoteMeunName();
            name.RoteName = db.systemRoteMeunName().get(e).RoteName;
            name.MenuName = db.systemRoteMeunName().get(e).MenuName;
            list.add(name);
        }
        JSONObject json = new JSONObject();
        json.put("list",list);
        renderJSON(json);
    }
    public static void roteCreate(){
        List<SystemRote> systemRoteList = new ArrayList<SystemRote>();
        int i=0;
        try{
            String rote = params.get("rote");
            if (rote.isEmpty()){
                systemRoteList = SystemRote.find("ROTENAME",rote).fetch();
                 i= systemRoteList.size();
                if (systemRoteList.isEmpty()){
                    SystemRote sr = new SystemRote();
                    sr.RoteID = systems.ids("ROTEID");
                    sr.RoteName = rote;
                    sr.save();

                }
                List<SystemRote> systemRoteLists = SystemRote.find("ROTENAME",rote).fetch();
                int j= systemRoteList.size();
                if (i>0){
                    renderJSON(1);
                }else{
                    renderJSON(-1);
                }
            }


        }catch (Exception ex){
            renderJSON(0);
        }
    }

    /**
     * 管理员设置
     */
    public static void admin(){
        List<AdminLogins> list = AdminLogins.findAll();
        render(list);
    }
    public static void Operation(){
        Long id = Long.parseLong(params.get("Id"));
        int AdminState =Integer.parseInt(params.get("AdminState"));
        AdminLogins adminLogins = AdminLogins.findById(id);
        adminLogins.AdminState=AdminState;
        adminLogins.save();
        admin();
    }


}
